//
//  XZTestDynamicVC.m
//  Teecloud
//
//  Created by PC on 2018/12/25.
//  Copyright © 2018年 小志. All rights reserved.
//

#import "XZTestDynamicVC.h"
#import "XZTestDynamicDetailVC.h"
#import "XZTestDynamicModel.h"
#import "XZTestDynamicCell.h"
@interface XZTestDynamicVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArr;
@property (nonatomic, strong) ZJTimeCountDown *timeCount;
@end

@implementation XZTestDynamicVC
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [UIApplication sharedApplication].idleTimerDisabled = YES;
}
-(void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    [UIApplication sharedApplication].idleTimerDisabled = NO;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"考试动态";
    self.view.backgroundColor = kWhiteColor;
    _timeCount = [[ZJTimeCountDown alloc] init];
    kWeakObject(self)
    [_timeCount zj_timeCountUPWithCurrentTime:0 interval:5 completeBlock:^(NSInteger hour, NSInteger minute, NSInteger second) {
         [weakself getTestMonitorList];
    }];
}
#pragma mark - Networking
- (void)getTestMonitorList{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setValue:FROM_IOS forKey:@"from"];
    [dict setValue:[XGMArchiverTools cacheForKey:BUSIORGID] forKey:@"busiOrgId"];
    [dict setValue:[XGMArchiverTools cacheForKey:USER_UserId] forKey:@"userId"];
    [dict setValue:[XGMArchiverTools cacheForKey:USER_identifierNumber] forKey:@"loginFinger"];
    kWeakObject(self)
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [HJNetwork POSTWithURL:[NSString stringWithFormat:@"/ajax-api.ashx?$c=TeeyunApp&$m=GetTestMonitorList&$v=%@",VERSION] parameters:dict cachePolicy:HJCachePolicyIgnoreCache callback:^(id responseObject, NSError *error) {
        [weakself.tableView.mj_header endRefreshing];
        [MBProgressHUD hideHUDForView:weakself.view animated:YES];
        if (!error) {
            if ([[responseObject objectForKey:RESULT_NAME_Success] boolValue]) {
                NSArray *dataInfo = [responseObject objectForKey:RESULT_NAME_Data];
                NSArray *array = [XZTestDynamicModel mj_objectArrayWithKeyValuesArray:dataInfo];
                [weakself.dataArr removeAllObjects];
                [weakself.dataArr addObjectsFromArray:array];
                if (weakself.dataArr.count == 0) {
                    [weakself.tableView setupEmptyDataText:@"暂无数据" secoundText:@"" buttonTit:@"" verticalOffset:0 emptyImage:NODATAIMG tapBlock:^{
                        
                    } butClick:^{
                        
                    }];
                }
                [weakself.tableView reloadData];
            }else{
                
                [MBProgressHUD showErrorOrSuc:[responseObject objectForKey:RESULT_NAME_Message] toView:weakself.view];
            }
            
        }else{
            [MBProgressHUD showErrorOrSuc:@"网络异常" toView:weakself.view];
        }
    }];
}

#pragma mark - TableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    XZTestDynamicCell *testDynamicCell = [tableView dequeueReusableCellWithIdentifier:@"XZTestDynamicCell"];
    XZTestDynamicModel * model = self.dataArr[indexPath.row];
    testDynamicCell.model = model;
    return testDynamicCell;
}
#pragma mark - TableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 64.f;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:true];
    XZTestDynamicDetailVC *detailVC = [[XZTestDynamicDetailVC alloc]init];
    XZTestDynamicModel * model = self.dataArr[indexPath.row];
    detailVC.testId = model.Id;
    [self.navigationController pushViewController:detailVC animated:true];
}
#pragma mark - Setter
- (NSMutableArray *)dataArr{
    if (!_dataArr) {
        _dataArr =[[NSMutableArray alloc]init];
    }
    return _dataArr;
}
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, DEVICE_WIDTH, DEVICE_HEIGHT-kBottomSafeHeight) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsVerticalScrollIndicator =false;
        _tableView.separatorStyle =UITableViewCellSeparatorStyleNone;
        //注册
        [_tableView registerNib:[UINib nibWithNibName:@"XZTestDynamicCell" bundle:nil] forCellReuseIdentifier:@"XZTestDynamicCell"];
        //上下拉刷新
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(getTestMonitorList)];
        [self.view addSubview:_tableView];
    }
    return _tableView;
}
- (void)dealloc {
    [_timeCount zj_timeDestoryTimer];
    NSLog(@"%@释放了",self.class);
}
@end
